#!./perl 


# ------------------------------------------------------------

# This script was baseed on the generic_mailer2.pl, by phil hooper (pjh@netcom.com)
# It was modified for the query engine behind the Catalyst Interlink Solaris Product Expo
# Dec 14, 1994 Marc Sacoolas

sub get_request {

    # Subroutine get_request reads the POST or GET form request from STDIN
    # into the variable  $request, and then splits it into its
    # name=value pairs in the associative array %rqpairs.
    # The number of bytes is given in the environment variable
    # CONTENT_LENGTH which is automatically set by the request generator.

    # Encoded HEX values and spaces are decoded in the values at this
    # stage.

    # $request will contain the RAW request. N.B. spaces and other
    # special characters are not handler in the name field.

    if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
    } elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
	$request = $ENV{'QUERY_STRING'};
    }


    @names = &url_decode(split(/[&=]/, $request));
    %rqpairs = @names;

}

sub url_decode {

#	Decode a URL encoded string or array of strings 
#		+ -> space
#		%xx -> character xx


    foreach (@_) {
	tr/+/ /;
	s/%(..)/pack("c",hex($1))/ge;
    }
    @_;
}

sub html_header {

    # Subroutine html_header sends to Standard Output the necessary
    # material to form an HHTML header for the document to be
    # returned, the single argument is the TITLE field.

    local($title) = @_;

    print "Content-type: text/html\n\n";
    print "<html><head>\n";
    print "<title>$title</title>\n";
    print "</head>\n<body>\n";
}

sub html_trailer {

    # subroutine html_trailer sends the trailing material to the HTML
    # on STDOUT.

    local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
	= gmtime;

    local($mname) = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
		     "Aug", "Sep", "Oct", "Nov", "Dec")[$mon];
    local($dname) = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
		     "Sat")[$wday]; 

    print "<p>\nGenerated by: <var>$0</var><br>\n";
    print "Date: $hour:$min:$sec UT on $dname $mday $mname $year.<p>\n";
    print "</body></html>\n";
}

# --------- Everything above here is generic ---------

# Define fairly-constants


#
# Get the input, output header
#

&get_request;


#
# make sure nobody tries to execute a subshell
#

$rqpairs{'mailto'} =~ s/~!/ ~!/g;

#
# check for REQUIRED keyword.  Set flag if value is required
# but not provided, then put up a page and forget about sending
# query.  The REQUIRED keyword was still used to insure a
# key word entry. 
#

@check_reqs = @names;
for $i (0..$#check_reqs){
    $name = shift(@check_reqs);
    $value = shift(@check_reqs);

    if ($name =~ /REQUIRED/) {
        if ($value eq "") {
            $bad = $name;
            $bad =~ s/\s*REQUIRED\s*//;
	    push(@missing, $bad);
	}
    }
}


if ($#missing >= 0) {
    &html_header('Need More Inoformation Notice');
    print "<H1>Missing Required Information</H1>\n";
    print "<HR>\n";
    print "<H3>Please provide values for the following:</H3>\n";
    print "<UL>\n";
    for $i (0..$#missing) {
        $field = shift(@missing);
        print "<LI> $field\n";
    }
    print "</UL>\n";
    print "<applet code=delay.class width=596 height=75>";
    print "<param name=img value=\"file:///tmp/httpd/.products/CDware/images/goto_home.gif\">";
    print "<param name=del value=\"4000\">";
    print "<param name=href value=\"http://localhost:7999/cgi-bin/req_more_info.pl?$rqpairs{'from'}+$rqpairs{'mailto'}\"></applet>";

    print "<HR>\n";
    print "<H3>Go back and try again</H3>\n";
 
    &html_trailer;
    exit 0;
}
#
# check and set print method with file flag--fax or email
#
if ($rqpairs{'submit'} eq "fax") {
     open (SUBMIT,">/tmp/httpd/fax_flag");
     close (SUBMIT);
} 

#
# mail info 
#

open (FILE, ">/tmp/httpd/info.$rqpairs{'submit'}");
        print FILE  "\n";
        print FILE  "   Catalyst CDware Product Information Request\n";
        print FILE  "\n";
             for $i (0..$#names){ 
       	     $name = shift(@names);
             $value = shift(@names);

             $i++;
             if ($i >= 5 && $name ne "" && $name ne "submit" ) {
             print FILE "$name:  $value\n";
             if ($name eq "Fax") { print FILE "\n"; break }   
             }
        }
        print FILE  "\n";
        print FILE  "Please send me more information on the following products:\n";
        print FILE  "---------------------------------------------------------\n";
        print FILE  "$rqpairs{'from'}\n";
 
close (FILE);

    print "Content-type: text/html\n\n";
    print "<html><head>\n";
    print "<title>Information Request Acknowledgement</title>\n";
    print "</head>\n<body>\n";
    print "<applet code=delay.class width=596 height=75>";
    print "<param name=img value=\"file:///tmp/httpd/.products/CDware/images/cdwareban.gif\">";
    print "<param name=del value=\"2200\">";
    print "<param name=href value=\"http://localhost:7999/$rqpairs{'from'}/CDware.frame.html\"></applet>";
    print "<p><p><center><hr>";
    print "<p><h1>Your information has been saved for </h1>";
    print "<p><h1>subsequent additional information requests.</h1>";
    print "<hr><H2>It will be E-mailed/Printed out when you end your CDware session.</h2></center>";
#    &html_trailer;


exit 0;

